<?php

function task_fillTab() {
    if ($GLOBALS['l_role'] >= 0) {
        $link = db_connect();
        if ($link) {
            $sql = "SELECT t.*, u.name, u2.name as name2 FROM task t join user u on u.ID = t.user_task_ID left join userstasks ut on t.ID = ut.task_doing_ID left join user u2 on ut.users_task_working_ID = u2.ID WHERE dateex >= CURDATE() ORDER BY dateex, ID";
            $result = mysqli_query($link, $sql);
            if ($result) {
                $int = 0;
                $rowid = 0;
                $row = mysqli_fetch_assoc($result);
                while($row) {
                    $int = $int+1;
                    if ($int % 2 == 0) {
                        echo('<tr class="lichy">');
                    } else {
                        echo('<tr class="sudy">');
                    }
                    $date_parts = preg_split("/[-]/", $row['dateex']);
                    $date_cor = $date_parts[2].".".$date_parts[1].".".$date_parts[0];
                    $id = $row['ID'];
                    $text = $row['text'];
                    $priorita = $row['priorita'];
                    $hotovo = $row['complete'];
                    $zadal = $row['name'];
                    $pracuje = $row['name2'];
                    $rowid = $row['ID'];
                    if ($row['name2'] == '') {
                        $rowid = -1;
                    }
                    if ($row) {
                        while ($rowid == $row['ID']) {
                            $row = mysqli_fetch_assoc($result);
                            if ($row) {
                                if ($rowid == $row['ID']) {
                                    $pracuje = $pracuje.", ".$row['name2'];
                                }
                            }
                        }
                    }
                    echo ("<td>".$id."</td><td>".$text."</td><td>".$priorita."</td><td>".$date_cor."</td><td>".$hotovo." %</td>");
                    echo ("<td>".$pracuje."</td><td>".$zadal."</td>");
                    echo ('<td class="upravit"><a class="tab" href="task_edit.php?q='.$id.'&txt='.$text.'&priorita='.$priorita.'&splnit='.$date_cor.'&hotovo='.$hotovo.'">uprav</a></td><td class="smazat"><a class="tab" href="task_del.php?q='.$id.'&text='.$text.'">del</a></td><td class="pracovat"><a class="tab" href="task.php?q='.$id.'&act=work">work</a></td><td class="koncit"><a class="tab" href="task.php?q='.$id.'&act=end">end</a></td>');
                    echo ("</tr>");
                    if ($rowid == -1) {$row = mysqli_fetch_assoc($result);}
                }
            }
        }
    }
}

function task_fillTab_man($query) {
    if ($GLOBALS['l_role'] >= 5) {
        $link = db_connect();
        if ($link) {
            $sql = "SELECT t.*, u.name, u2.name as name2 FROM task t join user u on u.ID = t.user_task_ID left join userstasks ut on t.ID = ut.task_doing_ID left join user u2 on ut.users_task_working_ID = u2.ID ORDER BY dateex, ID";
            $result = mysqli_query($link, $sql);
            if ($result) {
                $int = 0;
                $rowid = 0;
                $row = mysqli_fetch_assoc($result);
                while($row) {
                    $int = $int+1;
                    if ($int % 2 == 0) {
                        echo('<tr class="lichy">');
                    } else {
                        echo('<tr class="sudy">');
                    }
                    $date_parts = preg_split("/[-]/", $row['dateex']);
                    $date_cor = $date_parts[2].".".$date_parts[1].".".$date_parts[0];
                    $id = $row['ID'];
                    $text = $row['text'];
                    $priorita = $row['priorita'];
                    $hotovo = $row['complete'];
                    $zadal = $row['name'];
                    $pracuje = $row['name2'];
                    $rowid = $row['ID'];
                    if ($row['name2'] == '') {
                        $rowid = -1;
                    }
                    if ($row) {
                        while ($rowid == $row['ID']) {
                            $row = mysqli_fetch_assoc($result);
                            if ($row) {
                                if ($rowid == $row['ID']) {
                                    $pracuje = $pracuje.", ".$row['name2'];
                                }
                            }
                        }
                    }
                    echo ("<td>".$id."</td><td>".$text."</td><td>".$priorita."</td><td>".$date_cor."</td><td>".$hotovo." %</td>");
                    echo ("<td>".$pracuje."</td><td>".$zadal."</td>");
                    echo ('<td class="upravit"><a class="tab" href="task_edit.php?q='.$id.'&txt='.$text.'&priorita='.$priorita.'&splnit='.$date_cor.'&hotovo='.$hotovo.'">uprav</a></td><td class="smazat"><a class="tab" href="task_del.php?q='.$id.'&text='.$text.'">del</a></td><td class="pracovat"><a class="tab" href="task.php?q='.$id.'&act=work">work</a></td><td class="koncit"><a class="tab" href="task.php?q='.$id.'&act=end">end</a></td>');
                    echo ("</tr>");
                    if ($rowid == -1) {$row = mysqli_fetch_assoc($result);}
                }
            }
        }
    }
}

function task_fillTab_self() {
    if ($GLOBALS['l_role'] >= 0) {
        $link = db_connect();
        if ($link) {
            $sql = "SELECT t.*, u.name, u2.name as name2, u2.username FROM task t join user u on u.ID = t.user_task_ID left join userstasks ut on t.ID = ut.task_doing_ID left join user u2 on ut.users_task_working_ID = u2.ID ORDER BY dateex, ID";
            $result = mysqli_query($link, $sql);
            if ($result) {
                $int = 0;
                $rowid = 0;
                $row = mysqli_fetch_assoc($result);
                while($row) {
                    $vypis = false;
                    if ($int % 2 == 0) {
                        echo('<tr class="lichy">');
                    } else {
                        echo('<tr class="sudy">');
                    }
                    $date_parts = preg_split("/[-]/", $row['dateex']);
                    $date_cor = $date_parts[2].".".$date_parts[1].".".$date_parts[0];
                    $id = $row['ID'];
                    $text = $row['text'];
                    $priorita = $row['priorita'];
                    $hotovo = $row['complete'];
                    $zadal = $row['name'];
                    $pracuje = $row['name2'];
                    $rowid = $row['ID'];
                    if ($row['name2'] == '') {
                        $rowid = -1;
                    }
                    if ($row) {
                        while ($rowid == $row['ID']) {
                            if ($row['username'] == $_SESSION['username']){
                                $vypis = true;
                                $int = $int+1;
                            }
                            $row = mysqli_fetch_assoc($result);
                            if ($row) {
                                if ($rowid == $row['ID']) {
                                    $pracuje = $pracuje.", ".$row['name2'];
                                }
                            }
                        }
                    }
                    if ($vypis) {
                        echo ("<td>".$id."</td><td>".$text."</td><td>".$priorita."</td><td>".$date_cor."</td><td>".$hotovo." %</td>");
                        echo ("<td>".$pracuje."</td><td>".$zadal."</td>");
                        echo ('<td class="upravit"><a class="tab" href="task_edit.php?q='.$id.'&txt='.$text.'&priorita='.$priorita.'&splnit='.$date_cor.'&hotovo='.$hotovo.'">uprav</a></td><td class="smazat"><a class="tab" href="task_del.php?q='.$id.'&text='.$text.'">del</a></td><td class="koncit"><a class="tab" href="mywork.php?q='.$id.'&act=end">end</a></td>');
                        echo ("</tr>");
                    }
                    if ($rowid == -1) {$row = mysqli_fetch_assoc($result);}
                }
            }
        }
    }
}

function task_add($text, $priorita, $dateex, $hotovo) {
    if ($GLOBALS['l_role'] >= 0) {
        $link = db_connect();
        if ($link) {
            $sql_ins = "INSERT INTO task(user_task_ID, priorita, text, complete, dateex) VALUES((SELECT id FROM user WHERE username='".$_SESSION['username']."'), ".(int)$priorita.", '".htmlspecialchars($text, ENT_QUOTES)."', ".(int)$hotovo.", STR_TO_DATE('".htmlspecialchars($dateex, ENT_QUOTES)."','%d.%m.%Y'))";
            $result = mysqli_query($link, $sql_ins);
            if ($result) {
                db_close($link);
                return true;
            }
        } else {
            db_close($link);
            return false;
        }
    } else {
        return false;
    }
    return false;
}

function task_upd($id, $text, $priorita, $dateex, $hotovo) {
    if ($GLOBALS['l_role'] >= 0) {
        $link = db_connect();
        if ($link) {
            $sql_upd = "UPDATE task SET priorita=".(int)$priorita.", text='".htmlspecialchars($text, ENT_QUOTES)."', complete=".(int)$hotovo.", dateex=STR_TO_DATE('".htmlspecialchars($dateex, ENT_QUOTES)."','%d.%m.%Y') WHERE id=".$id;
            $result = mysqli_query($link, $sql_upd);
            if ($result) {
                db_close($link);
                return true;
            }
        } else {
            db_close($link);
            return false;
        }
    } else {
        return false;
    }
    return false;
}

function task_del($id) {
    if ($GLOBALS['l_role'] >= 5) {
        $link = db_connect();
        if ($link) {
            $sql_upd = "DELETE FROM task WHERE id=".$id." LIMIT 1";
            $result = mysqli_query($link, $sql_upd);
            if ($result) {
                db_close($link);
                return true;
            }
        } else {
            db_close($link);
            return false;
        }
    } else {
        return false;
    }
    return false;
}

function task_end($id) {
    if ($GLOBALS['l_role'] >= 0) {
        $link = db_connect();
        if ($link) {
            $sql_del = "DELETE FROM userstasks WHERE task_doing_ID=".(int)$id." AND users_task_working_ID=(SELECT id FROM user WHERE username='".$_SESSION['username']."')";
            $result = mysqli_query($link, $sql_del);
            if ($result) {
                db_close($link);
                return true;
            }
        } else {
            db_close($link);
            return false;
        }
    } else {
        return false;
    }
    return false;

}

function task_work($id) {
    if ($GLOBALS['l_role'] >= 0) {
        $link = db_connect();
        if ($link) {
            $sql_ins = "INSERT INTO userstasks(task_doing_ID, users_task_working_ID) VALUES(".(int)$id.",(SELECT id FROM user WHERE username='".$_SESSION['username']."'))";
            $result = mysqli_query($link, $sql_ins);
            if ($result) {
                db_close($link);
                return true;
            }
        } else {
            db_close($link);
            return false;
        }
    } else {
        return false;
    }
    return false;
}

?>
